//
//  ThesearchView.m
//  WE
//
//  Created by 要帅气的生活 on 15/11/20.
//  Copyright © 2015年 UNOHACHA. All rights reserved.
//

#import "ThesearchView.h"
#import "Size.h"
#import "Head.h"
#import <Masonry.h>
#import "BaseView.h"
#import "ToolUtil.h"
#import "ScanViewController.h"
#import "FoundViewController.h"
@interface ThesearchView ()<UISearchBarDelegate, UISearchDisplayDelegate,ScanQRCodeDelegate>

@property (nonatomic,strong) UIVisualEffectView *views;
@end

@implementation ThesearchView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _searchbar = [[UISearchBar alloc] initWithFrame:CGRectMake(15, 27, WindowWidth-60, 30)];
        
        BaseView *baseview = [[BaseView alloc] initWithFrame:CGRectMake(WindowWidth - 40, 27, 30, 30)];
        [self addSubview:baseview];
        baseview.imgaeview.image = [UIImage imageNamed:@"saomiao.png"];
        [baseview addTarget:self selector:@selector(saomiao)];
        _searchbar.placeholder = @"搜索医院，医生，科室，疾病";
        [self addSubview:_searchbar];
        _searchbar.delegate = self;
        //self.searchbar.showsScopeBar = YES;
        _searchbar.searchBarStyle = UISearchBarStyleMinimal; //选择样式
        
    }
    return self;
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    if (self.button == nil) {
        
//        self.button = [UIButton buttonWithType:UIButtonTypeCustom];
//        self.button.frame = CGRectMake(0, 64, WindowWidth, WindowHeight);
//        self.button.backgroundColor = [UIColor blackColor];
//        self.button.alpha = 0.5;
//        [self.button addTarget:self action:@selector(xiaoshi) forControlEvents:UIControlEventTouchUpInside];
//        //[self addSubview:self.button];
//        [self.superview addSubview:self.button];
        
        _views = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
        _views.frame = CGRectMake(0, 64, WindowWidth, WindowHeight);
        UITapGestureRecognizer *doubleClick = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(xiaoshi)];
        doubleClick.numberOfTapsRequired = 1;
        [_views addGestureRecognizer:doubleClick];
        [self.superview addSubview:_views];
        
    }else{
//        self.button.frame = CGRectMake(0, 64, WindowWidth, WindowHeight);
//        self.button.backgroundColor = [UIColor blackColor];
//        self.button.alpha = 0.5;
//        [self.button addTarget:self action:@selector(xiaoshi) forControlEvents:UIControlEventTouchUpInside];
//        //[self addSubview:self.button];
//        [self.superview addSubview:self.button];
        _views.frame = CGRectMake(0, 64, WindowWidth, WindowHeight);
        UITapGestureRecognizer *doubleClick = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(xiaoshi)];
        doubleClick.numberOfTapsRequired = 1;
        [_views addGestureRecognizer:doubleClick];
        [self.superview addSubview:_views];

    }
}
/* 找到第一响应者 回收键盘 */
- (void)xiaoshi
{
    //    UITextField *Field = [((UIView *)[self.search.subviews objectAtIndex:0]).subviews lastObject];
    //    [Field resignFirstResponder];
    NSLog(@"123");
    [_searchbar resignFirstResponder];
    //[_button removeFromSuperview];
    [_views removeFromSuperview];
    
    
}
/* searchBar搜索 */
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    /* 防止多个button叠加 */
    [self.button removeFromSuperview];
}
/* 找到VC 用于push 下一个VC  */
- (UIViewController *)viewController
{
    for (UIView *next = [self superview]; next; next = next.superview)
    {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]])
        {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}

-(void)saomiao{
    NSLog(@"扫描");
    ScanViewController * scanVC = [[ScanViewController alloc] init];
    scanVC.delegate = self;
    scanVC.view.backgroundColor = [UIColor whiteColor];
    [[self viewController].navigationController pushViewController:scanVC animated:YES];
    [self.button removeFromSuperview];
//    [_searchbar resignFirstResponder];
    [_views removeFromSuperview];

}
- (void)scanResult:(NSString *)result
{
    NSLog(@"%@",result);
}
@end
